package com.leetcode.search.backtracking;

import java.util.*;

/**
 * @author Dennis Li
 * @date 2020/7/27 19:05
 */
public class CombinationSum3_216 {

    private final List<List<Integer>> res = new ArrayList<>();
    private int k;

    public List<List<Integer>> combinationSum3(int k, int n) {
        this.k = k;
        backtrack(new LinkedList<>(), n, 1);
        return res;
    }

    private void backtrack(LinkedList<Integer> track, int target, int start) {
        if (0 == target && track.size() == k) {
            res.add(new ArrayList<>(track));
            return;
        }
        for (int i = start; i <= 9; i++) {
            if (track.contains(i) || i > target) continue;
            track.add(i);
            backtrack(track, target - i, i + 1);
            track.removeLast();
        }
    }

}
